package thread;
import java.util.Random;

public class TestDemo14 {

    public static long count;
    public static void main(String[] args) throws InterruptedException {
        int[] nums = new int[100_0000];
        Random random = new Random();
        for (int i = 0; i < nums.length; i++) {
            nums[i] = random.nextInt(100) + 1;
        }
        long begin = System.currentTimeMillis();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < nums.length; i+=2) {
                count += nums[i];
            }
        });
        t1.start();
        Thread t2 = new Thread(() -> {
            for (int i = 1; i < nums.length; i+=2) {
                count += nums[i];
            }
        });
        t2.start();
        t1.join();
        t2.join();

        long end = System.currentTimeMillis();
        System.out.println("总和为:" + (count));
        System.out.println("用了 " + (end - begin) + "ms");
    }
}
